package com.topvision.face.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by lucy on 2017/6/22.
 */

public class DrawRectView extends View {
    public DrawRectView(Context context) {
        this(context,null);
    }

    public DrawRectView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public DrawRectView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private boolean needDrawRect;
    private Paint paint;
    private HashMap<Rect, String> mRectHashMap;
    public void drawRect(Paint paint, HashMap<Rect, String> rectHashMap) {
        needDrawRect = true;
        this.paint = paint;
        mRectHashMap = rectHashMap;
        invalidate();
    }

    private boolean needDClear;
    public void clearDraw() {
        needDClear = true;
        invalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (needDrawRect) {
            Paint mPaint = new Paint();
            mPaint.setStrokeWidth(2);
            mPaint.setTextSize(30);
            mPaint.setColor(Color.WHITE);
            mPaint.setTextAlign(Paint.Align.LEFT);
            Set<Map.Entry<Rect, String>> set = mRectHashMap.entrySet();
            for (Map.Entry<Rect, String> entry : set) {
                Rect rect = entry.getKey();
                String name = entry.getValue();
                if (name == null) {
                    name = "不认识";
                }
                if (canvas != null) {
                    canvas.drawRect(rect, paint);
                    canvas.drawText(name, rect.left, rect.bottom + 40, mPaint);
                }
            }
            needDrawRect = false;
        } else if (needDClear) {
            canvas.drawColor(Color.TRANSPARENT);
            needDClear = false;
        }
    }

}
